using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.Script.Serialization;
namespace ihongma.Common
{
    public class SerializationHelper
    {
        public SerializationHelper() { }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="type">对象类型</param>
        /// <param name="filename">文件路径</param>
        /// <returns></returns>
        public static object Load(Type type, string filename)
        {
            FileStream fs = null;
            try
            {
                // open the stream...
                fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                XmlSerializer serializer = new XmlSerializer(type);
                return serializer.Deserialize(fs);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
        }
                /// <summary>
                /// json反序例化
                /// </summary>
                /// <typeparam name="T"></typeparam>
                /// <param name="JsonStr"></param>
                /// <returns></returns>
        public static List<T> JSONStringToList<T>(string JsonStr)
        {
            JavaScriptSerializer Serializer = new JavaScriptSerializer();
            List<T> objs = Serializer.Deserialize<List<T>>(JsonStr);
            return objs;
        }

        public static string jsontostring(string temp_json)
        {
            return temp_json = System.Text.RegularExpressions.Regex.Replace(temp_json, "e\\\":(\\d+)", match =>
             {
                 DateTime dt = new DateTime(1970, 1, 1);
                 dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
                 dt = dt.ToLocalTime();
                 return "e\":" + dt.ToString("yyyy-MM-dd HH:mm:ss");
             });
        }
        public static string jsontostringt(string temp_json)
        {
            return temp_json = System.Text.RegularExpressions.Regex.Replace(temp_json, "e\\\":(\\d+)", match =>
            {
                //DateTime dt = new DateTime(1970, 1, 1);
                //dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
                //dt = dt.ToLocalTime();
                return "e\":\"\\/Date(" + match.Groups[1].Value + ")\\/\"";
            });
        }
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="filename">文件路径</param>
        public static void Save(object obj, string filename)
        {
            FileStream fs = null;
            // serialize it...
            try
            {
                fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                XmlSerializer serializer = new XmlSerializer(obj.GetType());
                serializer.Serialize(fs, obj);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }

        }

    }
}
